package com.hzj.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzj.pojo.HospitalSet;
import com.hzj.pojo.vo.Ids;
import com.hzj.service.HospitalSetService;
import com.hzj.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 医院设置表 前端控制器
 * </p>
 *
 * @author hzj
 * @since 2021-04-16
 */

@Api(tags = "医院管理类，一些crud")
@RequestMapping("/provider/admin/hosp/hospitalSet")
@RestController
@CrossOrigin
public class HospitalSetController {
    @Autowired
    HospitalSetService hss;

    @ApiOperation(value = "带条件分页查询")
    @PostMapping("/getPageHosplist/{pageindex}/{pagesize}")
    public Result getPageHosplist(@PathVariable int pageindex, @PathVariable int pagesize, @RequestBody(required = false) HospitalSet hospitalSet) {
        IPage page = new Page(pageindex, pagesize);

        QueryWrapper wrapper = new QueryWrapper();
//        System.out.println("==========>" + hospitalSet);
        if (!StringUtils.isEmpty(hospitalSet)) {
            if (!StringUtils.isEmpty(hospitalSet.getHosname())) {
                wrapper.like("hosname", hospitalSet.getHosname());
            }
            if (!StringUtils.isEmpty(hospitalSet.getHoscode())) {
                wrapper.like("hoscode", hospitalSet.getHoscode());
            }
        }

//        wrapper.like("hoscode", hospitalSet.getHoscode());
        wrapper.eq("is_deleted", 0);
        IPage pageList = hss.page(page, wrapper);


        return Result.ok(pageList);
    }

    @ApiOperation(value = "查询所有（包括已删除的）")
    @RequestMapping("/getHospListAll")
    public Result<List<HospitalSet>> getHospListAll() {
        return Result.ok(hss.list());
    }

    @ApiOperation(value = "添加医院信息")
    @PostMapping("/addHosp")
    public Result addHosp(@RequestBody HospitalSet hospitalSet) {
        boolean save = hss.save(hospitalSet);
        return Result.ok(save);
    }

    @ApiOperation(value = "批量删除医院信息")
    @RequestMapping("/muchDelete")

    public Result deleteHospById(@RequestBody Ids ids) {

        System.out.println("ids========"+ids);

        List<HospitalSet> list = new ArrayList<>();
        for (Integer id : ids.getIds()) {
            HospitalSet hospitalSet = new HospitalSet();
            hospitalSet.setId(Long.valueOf(id));
            hospitalSet.setIsDeleted(1);
            list.add(hospitalSet);
        }
        boolean b = hss.updateBatchById(list);
        if (b) {
            return Result.ok();
        } else {
            return Result.fail();
        }
//        return Result.ok();
    }

    @ApiOperation(value = "修改")
    @PostMapping("/updateHosp")
    public Result uppdateHosp(@RequestBody HospitalSet hospitalSet) {
        boolean b = hss.updateById(hospitalSet);

        return Result.ok(b);

    }
}

